package ar.edu.unq.tpi.uis.tepe3.wicket.pages;

import org.apache.wicket.markup.html.form.ListMultipleChoice;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import ar.edu.unq.tpi.uis.tepe3.dominio.User;
import ar.edu.unq.tpi.uis.tepe3.dominio.exception.PersistenceException;
import ar.edu.unq.tpi.uis.tepe3.dominio.exception.UserCantBeCreated;
import ar.edu.unq.tpi.uis.tepe3.wicket.AbstractFormPage;
import ar.edu.unq.tpi.uis.tepe3.wicket.WicketApplication;

public class UserFormPage extends AbstractFormPage{
	
	public UserFormPage() {
		super("Crear un nuevo usuario");
        User aUser = new User("","",""); 
        setModel(new CompoundPropertyModel(aUser));
		initializeForm(UserListPage.class);
	}

	public UserFormPage (IModel aModel){
		super("Editar un usuario", aModel );
		//this.setModel(aModel);
		initializeForm(UserListPage.class);
	}

	@Override
	protected void addFields() {
        aForm.add(new FeedbackPanel("feedback"));
		aForm.add(new TextField("name", String.class));
		aForm.add(new TextField("id", String.class));
        aForm.add(new PasswordTextField("pass"));
        ListMultipleChoice listMult = new ListMultipleChoice("groups", ((WicketApplication) getApplication()).getModel().getGroups());
		aForm.add(listMult);
	}

	@Override
	protected void saveAction() {
		try {
			if(UserFormPage.this.isNew){
				((WicketApplication) UserFormPage.this.getApplication()).getModel().addUser((User) getModelObject());
			}
			((WicketApplication) UserFormPage.this.getApplication()).save();
			saveSucceed();
		} catch (Exception e) {
			warn(e.getMessage());
		}
	}

}
